<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Bosuppliercategory extends Controller_BoBase {
	public $auth_required = TRUE;

	public function action_index()
	{
		$supplier_category = ORM::factory("supplierCategory");
		$result = $supplier_category->where("status","=","A")->find_all();
		$view = View::factory("pages/bo/home");
		$this->setTab($view);
		$view->set_global('result', $result);
		$this->request->response = $view;
	}

	public function action_insert(){
		$supplier_category = ORM::factory("supplierCategory");
		$supplier_category->name = trim($_POST["name"]);
		$supplier_category->status = "A";
		if($supplier_category->save()){
			$view = View::factory("pages/bo/common/message_dialog");
			$view->message = "添加商家类别成功";
			exit($view->render());
		}
	}

	public function action_update(){
		$supplier_category = ORM::factory("supplierCategory",$_POST["id"]);
		$supplier_category->name = trim($_POST["name"]);
		if($supplier_category->save()){
			$view = View::factory("pages/bo/common/message_dialog");
			$view->message = "更新商家类别成功";
			exit($view->render());
		}
	}

	public function action_show(){
		$supplier_category = ORM::factory("supplierCategory");
		$result = $supplier_category->where("status","=","A")->find_all();
		$view = View::factory("pages/bo/supplier_category_page");
		$view->result = $result;
		$this->setTab($view);
		exit($view->render());
	}

	public function action_delete(){
		$supCateId = $_POST["cateid"];
		$status = "I";
		$supplier_category = ORM::factory("supplierCategory");
		$supplier_category->status = $status;
		$supplier_category->where("id","=",$supCateId);
		$result_supplier_category = $supplier_category->save_all();
		if($this->updateStatus($result_supplier_category,$status)){
			$view = View::factory("pages/bo/common/message_dialog");
			$view->message = "删除商家类别成功";
			exit($view->render());
		}
	}

	public function action_deleteAll(){
		$supplier_category = ORM::factory("supplierCategory");
		$supplier_category->status = "I";
		$ids = $_POST["ids"];
		foreach (split(",", $_POST["ids"]) as $id){
			if (!empty($id)){
				$supplier_category->or_where("id","=",$id);
			}
		}
		$result_supplier_category = $supplier_category->save_all();
		if($this->updateStatus($result_supplier_category,"I")){
			$view = View::factory("pages/bo/common/message_dialog");
			$view->message = "删除商家类别成功";
			exit($view->render());
		}
	}

	private function updateStatus($result_supplier_category,$status){
		if($result_supplier_category){
			$supplier = ORM::factory("supplier");
			$supplier->status = $status;
			foreach ($result_supplier_category->find_all() as $item){
				$supplier->or_where("sup_cate_id","=",$item->id);
			}
			$result_supplier = $supplier->save_all();

			if ($result_supplier){
				$dish_categoty = ORM::factory("dishCategory");
				$dish_categoty->status = $status;
				foreach ($result_supplier->find_all() as $item){
					$dish_categoty->or_where("supplierid","=",$item->id);
				}
				$result_dish_category = $dish_categoty->save_all();

				if ($result_dish_category){
					$dish = ORM::factory("dish");
					$dish->status = $status;
					foreach ($result_dish_category->find_all() as $subItem){
						$dish->or_where("dish_cate_id","=",$subItem->id);
					}
					return $dish->save_all();
				}
			}
		}
		return false;
	}
}